package com.hgyc.mom.generator;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.google.common.base.Strings;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

/**
 * mybatis-plug 代码生成工具
 *
 * @author fengweijian
 * @version 1.0.0
 * @since 2021/5/25 9:20
 */
public class CodeGenerator {
    //读取控制台内容
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help);
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (Strings.isNullOrEmpty(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    /**
     * 数据源配置
     */
    private static final DataSourceConfig.Builder DATA_SOURCE_CONFIG = new DataSourceConfig
            .Builder("jdbc:mysql://rm-7xv24azmf744kr066yo.mysql.rds.aliyuncs.com:3306/hgyc-mom?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8", "hgyc", "hgyc@2024")
            .schema("hgyc-mom");

    /**
     * 处理 all 情况
     */
    private static List<String> getTables(String tables) {
        return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    }

    public static void main(String[] args) {
        FastAutoGenerator.create(DATA_SOURCE_CONFIG)
                // 全局配置
                .globalConfig((scanner, builder) -> builder.author(scanner.apply("请输入作者名称？"))
                        .outputDir(System.getProperty("user.dir") + "/hgyc-generator/src/main/java")
                        .dateType(DateType.TIME_PACK) //设置日期类型
                        .disableOpenDir()
                        .enableSpringdoc()//swagger3
                )
                // 包配置
                .packageConfig((scanner, builder) -> builder.parent("com.hgyc.mom." + scanner.apply("请输入模块名？"))
                        .entity("api.entity")//定义entity生成包路径
                        .service("api.service")//定义service生成包路径
                )
                // 策略配置
                .strategyConfig((scanner, builder) -> builder
                        .addInclude(getTables(scanner.apply("请输入表名，多个英文逗号分隔？所有输入 all")))
                        .addTablePrefix("base_") // 设置过滤表前缀
                        .serviceBuilder().formatServiceFileName("%sService")//去掉Service的“I”前缀
                        .controllerBuilder().enableRestStyle().enableHyphenStyle()
                        .entityBuilder()
                        .idType(IdType.ASSIGN_ID)
                        .enableLombok()
                        .addTableFills(
                                // 配置自动填充的字段
                                new Column("create_time", FieldFill.INSERT),
                                new Column("create_by", FieldFill.INSERT),
                                new Column("update_time", FieldFill.UPDATE),
                                new Column("update_by", FieldFill.UPDATE)
                        )
                        .build())
                .templateEngine(new VelocityTemplateEngine())
                .execute();
    }
}
